from django.contrib.auth.models import User
from django.db import models
from tinymce.models import HTMLField
from db.base_model import BaseModel

# Create your models here.

class IT(BaseModel):
    """文章"""
    TYPE = (
        ('a', 'Web'),
        ('b', 'Python'),
        ('c', 'Linux'),
        ('d', 'Projects'),
        ('e', 'Other'),
    )
    title = models.CharField('标题', max_length=200, unique=True)
    intro = models.CharField('简介', max_length=100, default='None')
    body = HTMLField('正文')
    pub_time = models.DateTimeField(
        '发布时间',
        blank=False, null=False, auto_now_add=True)
    avatar = models.ImageField(default= 'Louis-LOGO.jpg',upload_to='Article_avatar', verbose_name='封面')
    type = models.CharField('分类', max_length=1, choices=TYPE, default='a')
    views = models.PositiveIntegerField('浏览量', default=0)
    like = models.PositiveIntegerField('点赞数', default=0)
    author = models.ForeignKey(
        User,
        verbose_name='作者',
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    article_order = models.IntegerField(
        '排序,数字越大越靠前', blank=False, null=False, default=0)
    tags = models.ManyToManyField('IT_Tag', verbose_name='标签集合', blank=True)

    def __str__(self):
        return self.title


    class Meta:
        db_table = "louis_it"
        verbose_name = "IT文章"
        verbose_name_plural = verbose_name
        get_latest_by = 'id'



class IT_Tag(BaseModel):
    """文章标签"""
    name = models.CharField('标签名', max_length=30, unique=True)
    slug = models.SlugField(default='no-slug', max_length=60, blank=True)

    def __str__(self):
        return self.name
